package com.searchanything;

import java.awt.List;
import java.util.HashMap;

import com.commonfunction.CommFunction;
import com.databasefunction.DatabaseConstants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SearchAnythingAction extends ActionSupport{
	String userrole;
	private String serchBy,searchText,startDate,endDate;
	
	
	public String getStartDate() {
		return startDate;
	}


	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}


	public String getEndDate() {
		return endDate;
	}


	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}


	public String getSerchBy() {
		return serchBy;
	}


	public void setSerchBy(String serchBy) {
		this.serchBy = serchBy;
	}


	public String getSearchText() {
		return searchText;
	}


	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}


	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		SearchAnythingFunctions searchAnythingFunctions = new SearchAnythingFunctions();
		CommFunction commFunction = new CommFunction();
		HashMap dealerData; 
		System.out.println(startDate);
		 
		if ((null != userrole) && ( userrole.equals("admin") || userrole.equals("superadmin"))) {
			
		}else
			return "unauthorised";
		
		
		dealerData= searchAnythingFunctions.getDealerData(searchText, serchBy.toLowerCase(),commFunction.formatDateForDb(startDate),commFunction.formatDateForDb(endDate));
		ActionContext.getContext().getSession().put("searchby", serchBy);
		ActionContext.getContext().getSession().put("searchtext", searchText);
		ActionContext.getContext().getSession().put("startDate", startDate);
		ActionContext.getContext().getSession().put("endDate", endDate);
		ActionContext.getContext().getSession().put("completeData", dealerData);
		return "success";
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		userrole = (String) ActionContext.getContext().getSession().get("userrole");
		//Load value from session will avoid form dupication error
		if(null == serchBy){
			serchBy = (String) ActionContext.getContext().getSession().get("searchby");
		}
		
		if(null == searchText){
			searchText = (String) ActionContext.getContext().getSession().get("searchtext");
		}
		
		if(null == startDate){
			startDate = (String) ActionContext.getContext().getSession().get("startDate");
		}
		
		if(null == endDate){
			endDate = (String) ActionContext.getContext().getSession().get("endDate");
		}
		
		if(null != serchBy && null != searchText ){
		CommFunction commFunction = new CommFunction();
		if (serchBy.equals("city") ) {
			if(!commFunction.isCityOrDistPresent(searchText, 0)){
				addActionError("Your search city not present");
			}
		}else if ( serchBy.equalsIgnoreCase("district")) {
			if(!commFunction.isCityOrDistPresent(searchText, 1)){
				addActionError("Your search district not present");
			}
		}else if(!commFunction.isDealerPresent(searchText)){
				addActionError("Your search dealer not present");
			}
		}else{
			addActionError("No Search data found");
		}
	}		
}

